/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

OutputWindow - 类名称
修改许可权限 - 修改人名称

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function OutputWindow()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
package editor.ui{
	import com.bit101.components.CheckBox;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import com.bit101.components.TextArea;
	import com.bit101.components.Window;
	
	import editor.common.Common;
	import editor.manager.MapManger;
	import editor.map.dataOutput.MapdataInputer;
	import editor.map.dataOutput.MapdataOutputer;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.IME;
	import flash.system.IMEConversionMode;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	
	public class OutputWindow extends Window{
		
		private var _mapIdLabel:Label;
		private var _mapId:InputText;
		
		private var _mapNameLabel:Label;
		private var _mapName:InputText;
		
		private var _outputTileBtn:PushButton;
		private var _inputXmlBtn:PushButton;
		private var _distantBtn:PushButton;
		
		public var fbCheckBox:CheckBox;
		
		public var distantFile:File;
		private var _isDistant:Boolean = false;
		
		
		// 构造函数
		public function OutputWindow(parent:DisplayObjectContainer=null, xpos:Number = 0, ypos:Number = 0)
		{
			super(parent, xpos, ypos, "文件");
			setSize(120,170);
			
			_mapIdLabel = new Label(this,10, 10, "地图id");
			_mapId = new InputText(this, 50, 10);
			_mapId.setSize(55,18);
			
			_mapNameLabel = new Label(this,10, 30, "地图名");
			_mapName = new InputText(this, 50, 30);
			_mapName.setSize(55,18);
			
//			_outputXmlBtn = new PushButton(this,10,52,"保存 xml", onOutputXml);
			_inputXmlBtn = new PushButton(this,10, 52, "打开", onInputXml);
			
			_outputTileBtn = new PushButton(this,10,73,"保存", onOutputTile);
			_distantBtn = new PushButton(this,10,95,"添加远景图", onAddDistant);
//			_inputTileBtn = new PushButton(this,10, 123, "导入行走图", onInputTile);
			
			fbCheckBox = new CheckBox(this, 10, 125, "是否为副本");
			fbCheckBox.selected = false;
		}
		
		private function onAddDistant(e:Event):void
		{
			distantFile = new File();
			distantFile.addEventListener(Event.SELECT, onDistantSelected);
			distantFile.browseForOpen("寻找远景图", [new FileFilter("*.jpg;*.jpeg;*.png", "*.jpg;*.jpeg;*.png")]);
			
		}
		
		private function onDistantSelected(e:Event):void
		{
			isDistant = true;
			
		}
				
		/**
		 * 显示地图名和id
		 * @param mapId
		 * @param mapName
		 */		
		public function showMapInfo(mapId:String, mapName:String):void
		{
			_mapName.text = mapName;
			_mapId.text = mapId;
		}
		
		private function onInputXml(evt:Event):void
		{
			var file:File = new File();
			file.addEventListener(Event.SELECT,onXmlFind);
			file.browseForDirectory("寻找地图文件夹");
		}
		
		private function onXmlFind(evt:Event):void
		{
			var file:File = evt.target as File;
			var pathStrs:Array = file.nativePath.split("/");
			var mapId:String = pathStrs[pathStrs.length - 1].split("_")[1];
			if(mapId == null)
				Alert.showLog("错误的路径");
			else
				MapdataInputer.getInstance().readMapData(mapId, file.nativePath);
		}
		
		//点击输出地图文件
		private function onOutputTile(evt:MouseEvent):void
		{
			if(_mapId.text == "" || _mapName.text == "")
				Alert.showLog("请输入地图名和地图id");
			else if(isDistant == false && fbCheckBox.selected == false)
				Alert.showLog("请添加远景图");
			else
			{
				Common.map.gmap.mapId = _mapId.text;
				Common.map.gmap.mapName = _mapName.text;
				MapManger.getInstance().tileLayer.outputTileData();
			}
			
		}
		
		//点击导入地图文件
		private function onInputTile(evt:MouseEvent):void
		{
			var file:File = new File();
			file.addEventListener(Event.SELECT,onTileFind);
			file.browseForOpen("寻找路径图", [new FileFilter("*.png", "*.png")]);
		}
		
		//载入地图路径图文件
		private function onTileFind(evt:Event):void
		{
			var file:File = evt.target as File;
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onTileLoaded);
			loader.load(new URLRequest(file.nativePath));
		}
		
		private function onTileLoaded(evt:Event):void
		{
			var loader:Loader = evt.target.loader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onTileLoaded);
			var bmp:BitmapData = Bitmap(loader.content).bitmapData;
			MapManger.getInstance().tileLayer.inputTileData(MapdataOutputer.readTileBitmapdata(bmp));
		}

		public function get isDistant():Boolean
		{
			return _isDistant;
		}

		public function set isDistant(value:Boolean):void
		{
			_isDistant = value;
			if(_isDistant)
				_distantBtn.label = "已添加，远景";
			else
				_distantBtn.label = "添加远景图";
		}

		//
	}
}